﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zyy;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zyy
{
    public class AddShelfdictionaryHandler : IRequestHandler<AddShelfdictionarycommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        /// <summary>
        ///  货架字典新增
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="db"></param>
        public AddShelfdictionaryHandler(IMapper mapper,ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(AddShelfdictionarycommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                var data = mapper.Map<Shelfdictionary>(request);
             
                result.Data=await db.Insertable(data).ExecuteCommandAsync();
                if(result.Data>0)
                {
                    result.Code = ApiEnums.添加成功;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Code = ApiEnums.添加失败;
                    result.Msg = "添加失败";
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
